/* Gauge - Performance Measurement Framework for Mobile Platforms
 * Copyright (C) 2013, Juha Hytonen <juha.a.hytonen@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.gauge;

/**
 * Measurement storage stores measurements produced by a benchmark.
 * 
 * Call this class' static methods to add measurements into the storage. If no 
 * implementation has been registered, an exception is thrown. Each benchmark
 * holds its own measurement storage.
 * 
 * Implement a concrete measurement storage by subclassing this class and overriding
 * the addMeasurement() method. Register your implementation by calling the Register()
 * method. The implementation is used for storing all successive measurements.
 */
public abstract class MeasurementStorage {
	
	private static MeasurementStorage storage;
	
	public final static void Register(MeasurementStorage instance) {
		storage = instance;
	}
	
	public final static boolean IsRegistered() {
		return (storage != null);
	}
	
	public final static void Close() throws Exception {
		checkIfStorageIsRegistered();
		storage.close();
		storage = null;
	}
	
	/**
	 * You can perform any necessary actions required when the measurement storage
	 * for a benchmark is closed (i.e. written on to disk).
	 */
	protected void close() throws Exception {
	}
	
	public static void Add(Measurement measurement) throws Exception {
		checkIfStorageIsRegistered();
		storage.addMeasurement(measurement);
	}
	
	/**
	 * You should add concrete measurements into the storage here.
	 */
	protected abstract void addMeasurement(Measurement measurement) throws Exception;
	
	private final static void checkIfStorageIsRegistered() throws Exception {
		if (storage == null) {
			throw new Exception("Measurement storage not registed");
		}
	}

}
